﻿using Bonjour;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DNSSDEventManager m_eventManager;
        private DNSSDService m_service;
        private DNSSDService m_registrar;
        public MainWindow()
        {
            InitializeComponent();
            InitBonjour();
        }

        void InitBonjour()
        {

            try
            {
                m_service = new DNSSDService();
            }
            catch
            {
                MessageBox.Show("Bonjour Service is not available", "Error");
                Application.Current.Shutdown();
            }

            m_eventManager = new DNSSDEventManager();
            m_eventManager.ServiceRegistered += new _IDNSSDEvents_ServiceRegisteredEventHandler(this.ServiceRegistered);
            m_eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
            m_eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
            m_eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
            m_eventManager.QueryRecordAnswered += new _IDNSSDEvents_QueryRecordAnsweredEventHandler(this.QueryAnswered);
            m_eventManager.OperationFailed += new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
       
			//
			// start asynchronous read
			//

			try
			{
				//
				// start the register and browse operations
				//
                m_registrar = m_service.Register(0, 0, "MapServerv2", "_http._tcp", null, null, (ushort)80, null, m_eventManager);
            }
            catch
            {
                MessageBox.Show("Bonjour service is not available", "Error");
                Application.Current.Shutdown();
            }
        }

        private void OperationFailed(DNSSDService service, DNSSDError error)
        {
            throw new NotImplementedException();
        }

        private void QueryAnswered(DNSSDService service, DNSSDFlags flags, uint ifIndex, string fullname, DNSSDRRType rrtype, DNSSDRRClass rrclass, object rdata, uint ttl)
        {
            throw new NotImplementedException();
        }

        private void ServiceResolved(DNSSDService service, DNSSDFlags flags, uint ifIndex, string fullname, string hostname, ushort port, TXTRecord record)
        {
            throw new NotImplementedException();
        }

        private void ServiceLost(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain)
        {
            throw new NotImplementedException();
        }
        
        private void ServiceFound(DNSSDService browser, DNSSDFlags flags, uint ifIndex, string serviceName, string regtype, string domain)
        {
            throw new NotImplementedException();
        }

        private void ServiceRegistered(DNSSDService service, DNSSDFlags flags, string name, string regtype, string domain)
        {
            MessageBox.Show("Service succesfully registered");
        }
    }
}
